package com.example.magichealth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Profil extends Activity implements OnClickListener {
	
	Button popupbutton1,popupbutton11,popupbutton111;
	Button popupbutton2,popupbutton22,popupbutton222;
	Button urg1,urg2,urg3;
	TextView popuptext,popuptext2,popuptext3;
	LinearLayout layoutpopup,layoutpopup2,layoutpopup3;
	PopupWindow popup,popup2,popup3;
	RelativeLayout table;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profil);
		initpopup1();
		popup1();
		initpopup2();
		popup2();
		initpopup3();
		popup3();
        	// Show the Up button in the action bar.
            setupActionBar();

            Intent intent = new  Intent();
        	intent.getComponent();
		
        
        //nom	
        try {
        	 FileInputStream fis = openFileInput("nom_file");
        	 try {
				byte[] b1 = new byte[20];
				fis.read(b1);
				fis.close();
				String nom = new String(b1);
				TextView text1 = (TextView)findViewById(R.id.strnom);
				text1.setText(nom);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //prenom
        try {
       	 FileInputStream fis = openFileInput("prenom_file");
       	 try {
				byte[] b2 = new byte[20];
				fis.read(b2);
				fis.close();
				String prenom = new String(b2);
				TextView text2 = (TextView)findViewById(R.id.strprenom);
				text2.setText(prenom);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
      //sexe
        try {
       	 FileInputStream fis = openFileInput("sexe_file");
       	 try {
				byte[] b3 = new byte[10];
				fis.read(b3);
				fis.close();
				String sexe = new String(b3);
				TextView text3 = (TextView)findViewById(R.id.strsexe);
				text3.setText(sexe);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //date de naissance(jour)
        try {
          	 FileInputStream fis = openFileInput("jour_file");
          	 FileInputStream fis1 = openFileInput("mois_file");
          	 FileInputStream fis2 = openFileInput("annee_file");
          	 try {
          		 
   				byte[] b4 = new byte[2];
   				fis.read(b4);
   				fis.close();
   				String jour = new String(b4);
   				
   				byte[] b5 = new byte[2];
   				fis1.read(b5);
   				fis1.close();
   				String mois = new String(b5);
   				
   				byte[] b6 = new byte[4];
   				fis2.read(b6);
   				fis2.close();
   				String annee = new String(b6);
   				
   				TextView text4 = (TextView)findViewById(R.id.strdate);
   				text4.setText(jour+"/"+mois+"/"+annee);
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
        
      //lieu de naissance
        try {
       	 FileInputStream fis = openFileInput("lieu_file");
       	 try {
				byte[] b7 = new byte[30];
				fis.read(b7);
				fis.close();
				String lieu = new String(b7);
				TextView text5 = (TextView)findViewById(R.id.strlieuNaissance);
				text5.setText(lieu);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
        //poids
        try {
          	 FileInputStream fis = openFileInput("poids_file");
          	 try {
   				byte[] b8 = new byte[4];
   				fis.read(b8);
   				fis.close();
   				String poids = new String(b8);
   				TextView text5 = (TextView)findViewById(R.id.strpoids);
   				text5.setText(poids+" kg");
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
		
      //taille
        try {
          	 FileInputStream fis = openFileInput("taillem_file");
          	 FileInputStream fis1 = openFileInput("taillecm_file");
          	 try {
   				byte[] b91 = new byte[3];
   				fis.read(b91);
   				fis.close();
   				String taillemetre = new String(b91);
   				
   				byte[] b92 = new byte[2];
   				fis1.read(b92);
   				fis1.close();
   				String taillecm = new String(b92);
   				
   				TextView text5 = (TextView)findViewById(R.id.strtaille);
   				text5.setText(taillemetre+"m"+taillecm);
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
        
      //personne urgence 1
        //PENSER A RELIER CELA VERS LE REPERTOIRE TELEPHONIQUES
        try {
          	 FileInputStream fis = openFileInput("nomurgence1_file");
          	FileInputStream fich = openFileInput("telurgence1_file");
          	 try {
   				byte[] b10 = new byte[20];
   				fis.read(b10);
   				fis.close();
   				String nomurgence1 = new String(b10);
   				
   				byte[] b1 = new byte[15];
				fich.read(b1);
				fich.close();
				String telurgence1 = new String(b1);
				
   				Button b2 = (Button)findViewById(R.id.urgence1);
   				if ((!nomurgence1.matches(""))&&(!telurgence1.matches("")))
   				{
   				b2.setText(nomurgence1+" "+"--"+" "+"Tel:"+telurgence1);
   				}
   				else
   	   		     b2.setText("Contact Urgence 1");
   	   				  
          	 } catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}	
         
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
        
      //personne urgence 2
        //PENSER A RELIER CELA VERS LE REPERTOIRE TELEPHONIQUES
        try {
        	 FileInputStream fis = openFileInput("nomurgence2_file");
           	FileInputStream fich = openFileInput("telurgence2_file");
           	 try {
    				byte[] b10 = new byte[20];
    				fis.read(b10);
    				fis.close();
    				String nomurgence2 = new String(b10);
    				
    				byte[] b1 = new byte[15];
 				fich.read(b1);
 				fich.close();
 				String telurgence2 = new String(b1);
 				
    				Button b2 = (Button)findViewById(R.id.urgence2);
    				if ((!nomurgence2.matches(""))&&(!telurgence2.matches("")))
    				{
    				b2.setText(nomurgence2+" "+"--"+" "+"Tel:"+telurgence2);
    				}
    				else
    	   		     b2.setText("Contact Urgence 2");
   				
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
        
      //medecin
        //PENSER A RELIER CELA VERS LE REPERTOIRE TELEPHONIQUES
        try {
          	 FileInputStream fis = openFileInput("nommedecin_file");
          	 FileInputStream fich = openFileInput("telmedecin_file");
          	try {
           		byte[] b10 = new byte[20];
   				fis.read(b10);
   				fis.close();
   				String nommedecin = new String(b10);
    				
    				byte[] b1 = new byte[15];
 				fich.read(b1);
 				fich.close();
 				String telmedecin = new String(b1);
 				
 				Button b2 = (Button)findViewById(R.id.medecin);
    				if ((nommedecin.length()!=0)&&(telmedecin.length()!=0))
    				{
    				b2.setText(nommedecin+" "+"--"+" "+"Tel:"+telmedecin);
    				}
    				else
    	   		     b2.setText("Votre Medecin");
   				
   			} catch (IOException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
   		} catch (FileNotFoundException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
        
      //infos
        try {
       	 FileInputStream fis = openFileInput("infos_file");
       	 try {
				byte[] b3 = new byte[300];
				fis.read(b3);
				fis.close();
				String infos = new String(b3);
				TextView text3 = (TextView)findViewById(R.id.strinformations);
				text3.setText(infos);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}
	
	
	
	 //POPUP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	public void initpopup1() //initialisation de la fenetre popup
	{
		urg1 = (Button)findViewById(R.id.urgence1);
		popupbutton1 = new Button (this);
		popupbutton2 = new Button (this);
		popuptext = new TextView (this);
		layoutpopup = new LinearLayout(this);
		
		try {
				FileInputStream fis = openFileInput("telurgence1_file");
				try {
					byte[] b10 = new byte[15];
					fis.read(b10);
					fis.close();
					String telurgence1 = new String(b10);
					popuptext.setText("Appeler le "+telurgence1+" ?");
  				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		popuptext.setPadding(0, 0, 0, 20);	
		popupbutton1.setText("Appeler");
		popupbutton2.setText("Retour");
		layoutpopup.setOrientation(1);
		layoutpopup.addView(popuptext);
		layoutpopup.addView(popupbutton1);
		layoutpopup.addView(popupbutton2);
		
	}
	
	public void initpopup2() //initialisation de la fenetre popup
	{
		urg2 = (Button)findViewById(R.id.urgence2);
		popupbutton11 = new Button (this);
		popupbutton22 = new Button (this);
		popuptext2 = new TextView (this);
		layoutpopup2 = new LinearLayout(this);
		
		try {
				FileInputStream fis = openFileInput("telurgence2_file");
				try {
					byte[] b10 = new byte[15];
					fis.read(b10);
					fis.close();
					String telurgence2 = new String(b10);
					popuptext2.setText("Appeler le "+telurgence2+" ?");
  				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		popuptext2.setPadding(0, 0, 0, 20);	
		popupbutton11.setText("Appeler");
		popupbutton22.setText("Retour");
		layoutpopup2.setOrientation(1);
		layoutpopup2.addView(popuptext2);
		layoutpopup2.addView(popupbutton11);
		layoutpopup2.addView(popupbutton22);
		
	}
	
	public void initpopup3() //initialisation de la fenetre popup
	{
		urg3 = (Button)findViewById(R.id.medecin);
		popupbutton111 = new Button (this);
		popupbutton222 = new Button (this);
		popuptext3 = new TextView (this);
		layoutpopup3 = new LinearLayout(this);
		
		try {
				FileInputStream fis = openFileInput("telmedecin_file");
				try {
					byte[] b10 = new byte[15];
					fis.read(b10);
					fis.close();
					String telmedecin = new String(b10);
					popuptext3.setText("Appeler le "+telmedecin+" ?");
  				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		popuptext3.setPadding(0, 0, 0, 20);	
		popupbutton111.setText("Appeler");
		popupbutton222.setText("Retour");
		layoutpopup3.setOrientation(1);
		layoutpopup3.addView(popuptext3);
		layoutpopup3.addView(popupbutton111);
		layoutpopup3.addView(popupbutton222);
		
	}

	
	public void popup1(){
		urg1.setOnClickListener(this);
		popupbutton1.setOnClickListener(this);
		popupbutton2.setOnClickListener(this);
		popupbutton1.setId(1);
		
		popup= new PopupWindow(layoutpopup, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popup.setContentView(layoutpopup);
		
		
	}
	
	public void popup2(){
		urg2.setOnClickListener(this);
		popupbutton11.setOnClickListener(this);
		popupbutton22.setOnClickListener(this);
		popupbutton11.setId(2);
		
		popup2= new PopupWindow(layoutpopup2, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popup2.setContentView(layoutpopup2);
		
		
	}
	
	public void popup3(){
		urg3.setOnClickListener(this);
		popupbutton111.setOnClickListener(this);
		popupbutton222.setOnClickListener(this);
		popupbutton111.setId(3);
		
		popup3= new PopupWindow(layoutpopup3, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popup3.setContentView(layoutpopup3);
		
		
	}
	
		
		
		
		
		
	
	public void call1(View view){
		String tel = "0101010101";
		try {
			FileInputStream fis = openFileInput("telurgence1_file");
			try {
				byte[] b10 = new byte[15];
				fis.read(b10);
				fis.close();
				String tel1 = new String(b10);
				tel=tel1;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:" + tel));
		startActivity(callIntent);
	}
	
	public void call2(View view){
		String tel = "0101010101";
		try {
			FileInputStream fis = openFileInput("telurgence2_file");
			try {
				byte[] b10 = new byte[15];
				fis.read(b10);
				fis.close();
				String tel1 = new String(b10);
				tel=tel1;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:" + tel));
		startActivity(callIntent);
	}
	
	public void call3(View view){
		String tel = "0101010101";
		try {
			FileInputStream fis = openFileInput("telmedecin_file");
			try {
				byte[] b10 = new byte[15];
				fis.read(b10);
				fis.close();
				String tel1 = new String(b10);
				tel=tel1;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:" + tel));
		startActivity(callIntent);
	}
	
	public void goToModifyProfile(View view) {
		Intent intent1 = new Intent(this, ModifyProfile.class);
		startActivity(intent1);
	}
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.profil, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.urgence1)
		{
			popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.WHITE));
			popup.showAtLocation(layoutpopup3, Gravity.CENTER, 10, 10);
			//popup.showAsDropDown(urg1,0,0);
			table = (RelativeLayout) findViewById(R.id.table);
			//table.setVisibility(View.GONE);
			
		}
		else
		
			if(v.getId()==R.id.urgence2)
		{
			
			popup2.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.WHITE));
			popup2.showAtLocation(layoutpopup3, Gravity.CENTER, 10, 10);
			//popup2.showAsDropDown(urg2,0,0);
			table = (RelativeLayout) findViewById(R.id.table);
			
		}
		
			else
			
				if(v.getId()==R.id.medecin)
			{
					popup3.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.WHITE));
					popup3.showAtLocation(layoutpopup3, Gravity.CENTER, 10, 10);
					
					//popup3.showAsDropDown(urg3,0,0);
				table = (RelativeLayout) findViewById(R.id.table);
				//table.setVisibility(View.GONE);
				
			}
			else
					if(v.getId()==1)
					{
						call1(v);
					}
			else
				if(v.getId()==2)
					{
						call2(v);
					}
			
			else
				if(v.getId()==3)
					{
						call3(v);
					}
			else
				{
					popup.dismiss();
					popup2.dismiss();
					popup3.dismiss();
					table = (RelativeLayout) findViewById(R.id.table);
					table.setVisibility(View.VISIBLE);
				}
			
		//}
	}

}
